/**
 * 
 */

package com.wutianyi.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @author wutianyi
 * 
 */
public class Server {

	public static void main(String[] args) throws InterruptedException {

		try {
			final Selector selector = Selector.open();

			final ServerSocketChannel ssc = ServerSocketChannel.open();
			ssc.configureBlocking(false);
			final ServerSocket ss = ssc.socket();
			InetSocketAddress address = new InetSocketAddress(8888);
			ss.bind(address);
			SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);

			Thread thread_2 = new Thread(new Runnable() {

				@Override
				public void run() {

					System.out.println("Begin handler request!");
					while(true) {
						try {
							selector.select();
							Set<SelectionKey> keys = selector.selectedKeys();
							System.out.println(keys.size());
							for(Iterator<SelectionKey> itr = keys.iterator(); itr.hasNext();) {
								SelectionKey key = itr.next();
								if(!key.isValid()) {
									key.cancel();
								}
								if(key.isAcceptable()) {
									ServerSocketChannel server = (ServerSocketChannel)key.channel();
									SocketChannel sc = server.accept();
									sc.configureBlocking(false);
									sc.register(selector, SelectionKey.OP_READ);
								}
								if(key.isReadable()) {
									SocketChannel client = (SocketChannel)key.channel();
									ByteBuffer buffer = ByteBuffer.allocate(1024);
									byte[] b = new byte[1024];
									int len = client.read(buffer);
									buffer.flip();
									buffer.get(b, 0, len);

									System.out.println(new String(b, 0, len));
								}

							}
							keys.clear();
						}catch(IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}
			});
			thread_2.start();
			Thread thread = new Thread(new Runnable() {

				@Override
				public void run() {

					while(true) {
						try {
							System.out.println("Begin Accept Connection!");
							SocketChannel client = ss.accept().getChannel();
							if(null == client) {
								System.out.println("client");
								continue;
							}
							System.out.println("Accept a connection!");
							SelectableChannel channel = client;
							channel.configureBlocking(false);
							SelectionKey k =
									channel.register(selector, SelectionKey.OP_READ, Math.random());
						}catch(IOException e) {
							// TODO Auto-generated catch block
							System.out.println("finish");
							e.printStackTrace();
						}
					}

				}
			});
			// thread.start();
			// thread.join();
			// SelectionKey key = ssc.register(selector,
			// SelectionKey.OP_ACCEPT);
			//			
			// int num = selector.select();
			//			
			// Set<SelectionKey> selectedKey = selector.selectedKeys();
			// Iterator<SelectionKey> it = selectedKey.iterator();
			//			
			// while(it.hasNext()) {
			// SelectionKey k = it.next();
			// if((k.readyOps() & SelectionKey.OP_ACCEPT) ==
			// SelectionKey.OP_ACCEPT) {
			// System.out.println("Receieve a connection!");
			// }
			// }
		}catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
